package com.cn.medicalcare.service.serviceimpl;

import com.cn.medicalcare.customtool.JedisUtil;
import com.cn.medicalcare.customtool.JsonTool;
import com.cn.medicalcare.customtool.SqlSessionFactoryTool;
import com.cn.medicalcare.mapper.NavigationMapper;
import com.cn.medicalcare.mapper.NewsCategoryMapper;
import com.cn.medicalcare.pojo.AllPage;
import com.cn.medicalcare.pojo.NewsCategory;
import com.cn.medicalcare.service.NewsCategoryService;
import com.fasterxml.jackson.core.type.TypeReference;
import org.apache.commons.lang3.StringUtils;

import java.util.List;

public class NewsCategoryServiceImpl implements NewsCategoryService {

    private static final String NEWS_CATE_CACHE_KEY = "survival_news_category";

    //静态内部类单例模式
    private static class SingletonClassInstance {
        private static final NewsCategoryServiceImpl instance = new NewsCategoryServiceImpl();
    }

    private NewsCategoryServiceImpl() {
    }

    public static NewsCategoryServiceImpl getInstance() {
        return NewsCategoryServiceImpl.SingletonClassInstance.instance;
    }

    @Override
    public List<NewsCategory> selectNewsCategoryAll() {
        List<NewsCategory> list = null;
        try {
            String newsCateJson = JedisUtil.jedisDo(j -> j.get(NEWS_CATE_CACHE_KEY));
            if (StringUtils.isNotEmpty(newsCateJson)) {
                list = JsonTool.toCollection(newsCateJson, new TypeReference<List<NewsCategory>>() {
                });
                JedisUtil.jedisDo(j -> j.expire(NEWS_CATE_CACHE_KEY, JedisUtil.CACHE_TIME));
            }
        } catch (Exception e) {
        }
        if (list == null) {
            list = SqlSessionFactoryTool.DoMapper(NewsCategoryMapper.class, m -> m.selectNewsCategoryAll());
            try {
                String newsCateJson = JsonTool.toJson(list);
                JedisUtil.jedisDo(j -> j.setex(NEWS_CATE_CACHE_KEY, JedisUtil.CACHE_TIME, newsCateJson));
            } catch (Exception e) {
            }
        }
        return list;
    }

    @Override
    public List<NewsCategory> selectNewsCategoryInPage(AllPage<NewsCategory> allPage) {
        return SqlSessionFactoryTool.DoMapper(NewsCategoryMapper.class, m -> m.selectNewsCategoryInPage(allPage));
    }

    @Override
    public int selectNewsCategoryCount(NewsCategory newsCategory) {
        return SqlSessionFactoryTool.DoMapper(NewsCategoryMapper.class, m -> m.selectNewsCategoryCount(newsCategory));
    }

    @Override
    public boolean insertNewsCategory(NewsCategory newsCategory) {
        boolean insertFlag = SqlSessionFactoryTool.DoMapperAndCommit(NewsCategoryMapper.class, m -> m.insertNewsCategory(newsCategory));
        if (insertFlag) {
            JedisUtil.jedisDo(j -> j.del(NEWS_CATE_CACHE_KEY));
        }
        return insertFlag;
    }

    @Override
    public boolean updateNewsCategory(NewsCategory newsCategory) {
        boolean updateFlag = SqlSessionFactoryTool.DoMapperAndCommit(NewsCategoryMapper.class, m -> m.updateNewsCategory(newsCategory));
        if (updateFlag) {
            JedisUtil.jedisDo(j -> j.del(NEWS_CATE_CACHE_KEY));
        }
        return updateFlag;
    }

}
